package com.suikajy.damaiproject.ui.main_class;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.suikajy.damaiproject.R;
import com.suikajy.damaiproject.bean.ClassBean;
import com.suikajy.damaiproject.config.UrlConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by suikajy on 2016/12/12.
 */

public class ClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<ClassBean.LBean> mList = new ArrayList<>();
    private LayoutInflater mInflate;
    public static final int NORMAL_ITEM_TYPE = 1;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mInflate == null)
            mInflate = LayoutInflater.from(parent.getContext());
        RecyclerView.ViewHolder vh = null;
        View itemView;
        switch (viewType) {
            case NORMAL_ITEM_TYPE:
                itemView = mInflate.inflate(R.layout.item_class, parent, false);
                vh = new VH(itemView);
                break;
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VH) {
            ClassBean.LBean bean = mList.get(position);
            VH vh = (VH) holder;
            vh.mSdvImage.setImageURI(UrlConfig.getImageUrl(bean.getI()));
            vh.mTvN.setText(bean.getN());
            vh.mTvP.setText(bean.getP());
            vh.mTvT.setText(bean.getT());
            vh.mTvVenName.setText(bean.getVenName());
            if (bean.getIsXuanZuo() == 1) {
                vh.mTvIsXuanZuo.setVisibility(View.VISIBLE);
                vh.mTvShouPiao.setVisibility(View.VISIBLE);
                vh.mTvYuShouZhong.setVisibility(View.GONE);
            } else {
                vh.mTvYuShouZhong.setVisibility(View.VISIBLE);
                vh.mTvShouPiao.setVisibility(View.GONE);
                vh.mTvIsXuanZuo.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return NORMAL_ITEM_TYPE;
    }

    public void setData(ClassBean bean) {
        mList.clear();
        mList.addAll(bean.getL());
        notifyDataSetChanged();
    }

    public void addData(ClassBean bean) {
        mList.addAll(bean.getL());
        notifyDataSetChanged();
    }

    class VH extends RecyclerView.ViewHolder {
        @Bind(R.id.sdv_image)
        SimpleDraweeView mSdvImage;
        @Bind(R.id.tv_n)
        TextView mTvN;
        @Bind(R.id.tv_is_xuan_zuo)
        TextView mTvIsXuanZuo;
        @Bind(R.id.tv_shou_piao)
        TextView mTvShouPiao;
        @Bind(R.id.tv_yu_shou_zhong)
        TextView mTvYuShouZhong;
        @Bind(R.id.tv_t)
        TextView mTvT;
        @Bind(R.id.tv_ven_name)
        TextView mTvVenName;
        @Bind(R.id.tv_p)
        TextView mTvP;
        @Bind(R.id.ll_price)
        LinearLayout mLlPrice;

        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
